/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Holds the data of one entire list item.  The model uses this to hold an entire list item.
 *
 */

package Decision_Lists;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 */
public class List_Item_Data {

  // Nested Class ProCon:  Holds all data of one "pro" (good feature) or one "con" (bad feature) of a list item
  public class ProCon {  
    //////////////////////////
    //// Nested class Vars ///
    String mainPhrase;  // one word or short phrase that is the main idea of the pro
    List<String> detailsList = new LinkedList<String>();

    // nested class constructor
    public ProCon() {
      
    }
    //////////////////////////
    /// GETTERS & SETTERS ////
    public void addDetail( String detail ) {
      detailsList.add(detail);
    }
    public void removeDetail(String detail) {
      detailsList.remove(detail);
    }
    public List<String> getDetailsList() {
      return detailsList;
    }
    public void setDetailsList(List<String> details) {
      this.detailsList = details;
    }
    public String getMainPhrase() {
      return mainPhrase;
    }
    public void setMainPhrase(String mainPhrase) {
      this.mainPhrase = mainPhrase;
    }
  } /////// end nested class ProCon //////////////////


  //////////////////////////////////
  ///// Outer Class Vars ///////////
  protected String name;
  protected int price;
  protected String summary;

  protected final String DEFAULT_PIC_FILENAME = "media/DefaultHouseImage.jpg";

  ImagePanel mainPicPanel; // ImagePanel: Nested class for making Images into JPanels for easier layouts
                           // The setter func setMainPic() takes a filename of a pic and puts it into a JPanel
  
  ImageIcon mainPicImageIcon; // Need this form of the pic for SpreadsheetView's JTable

  ImagePanel locationPicPanel;
  ImageIcon  locationPicImageIcon;

  List <ProCon> pros = new LinkedList<ProCon>();
  List <ProCon> cons = new LinkedList<ProCon>();

  ////// end Class Vars //////////
  ////////////////////////////////

  // Constructor
  public List_Item_Data()  {


  } ////// end constructor //////


  // Returns an ImageIcon, or the default image if cannot get user's img or exits if unsucceful at either.
  protected ImageIcon createImageIcon( String path ) {

    //java.net.URL imgURL = getClass().getResource(path);
    ImageIcon imageIcon = new ImageIcon(path);

    if(imageIcon != null) {
      return imageIcon;
    }
    else {
      System.err.println("Couldn't find file: \"" + path + "\", using default image.");
      imageIcon = new ImageIcon(DEFAULT_PIC_FILENAME);
      if(imageIcon != null)
        return imageIcon;
      else {
        System.err.println("Couldn't load default image either, exiting.");
        System.exit(-1);
      }
      return null; // (unreachable)
    }
  } //// end creaetImageIcon() //////

  ///////////////////////////////////
  ////// GETTERS AND SETTERS ////////

  public String getName() {    return name;  }

  public void setName(String name) {    this.name = name;  }

  public int getPrice() {    return price;  }

  public void setPrice(int price) {    this.price = price;  }

  public String getSummary() {    return summary;  }

  public void setSummary(String summary) {    this.summary = summary;  }

  public BufferedImage getMainPicBufferedImage() {    return mainPicPanel.image;  }

  public ImagePanel getMainPicPanel() {    return mainPicPanel;  }

  public void setMainPic(String mainPicFilename) throws IOException {
    mainPicPanel = new ImagePanel(mainPicFilename); // nested class checks for error, etc.

    mainPicImageIcon = createImageIcon(mainPicFilename); // func checks for error.

  } ///// end setMainPic() //////////

  public BufferedImage getLocationPicBufferedImage() {    return locationPicPanel.image;  }

  public ImagePanel getLocationPicPanel() {    return locationPicPanel;  }

  public void setLocationPic(String locationPicFilename) throws IOException {
    locationPicPanel = new ImagePanel(locationPicFilename); // nested class checks for error, etc.

    locationPicImageIcon = createImageIcon(locationPicFilename); // func checks for error.

  } ///// end setLocationPic() //////////
  

  public void addPro(ProCon pro) {
    pros.add(pro);
  }
  public void addCon(ProCon con) {
    cons.add(con);
  }
  public List<ProCon> getPros() {
    return pros;
  }
  public List<ProCon> getCons() {
    return cons;
  }

  public ImageIcon getMainPicImageIcon() {
    return mainPicImageIcon;
  }



  
  /////////////////////////////////////////////////////////////////////////////
  // Nested class for making Images into JPanels for easier layouts
  // Got idea from: http://stackoverflow.com/questions/299495/java-swing-how-to-add-an-image-to-a-jpanel
  private class ImagePanel extends JPanel{

    private BufferedImage image;
    // Constructor
    public ImagePanel(String picFilename) throws IOException {
      // Try to open the user's image. If that fails, try to open the default image. If that fails, exit.
      try {
          image = ImageIO.read(new File(picFilename));
       } catch (IOException e) {
            System.err.println(e);
            System.err.println("\nCould not open your pic file \"" + picFilename
                               + "\", using a default image instead.");

            try {
              image = ImageIO.read(new File(DEFAULT_PIC_FILENAME));
            } catch (IOException ex) {
                System.err.println(ex);
                System.err.println("\nCould not open the default pic file, exiting.");
                System.exit(-1);
            } // end inner catch
       } // end outer catch

    } // end constructor

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);  // args: image, x, y, ImageObserver
    }

  } //// end nested class ImagePanel ///////////////////////

} //////////////////// end class List_Item_Data /////////////////////////////
